import { configureStore, combineReducers } from '@reduxjs/toolkit'
import counterRouter from './modules/router'
import counterSlice from './modules/counterSlice'
import getDataConfig from './modules/getDataConfig.ts'
import { persistStore, persistReducer } from 'redux-persist'
// 选择持久化存储引擎，如 localStorage 或 AsyncStorage

import storageSession from 'redux-persist/lib/storage/session'
// 组合各个模块的reducer
const reducer = combineReducers({
  counter: counterRouter,
  contText:counterSlice,
  getDataConfig:getDataConfig
})
 
// 配置持久化设置
const persistConfig = {
  key: 'root', // 存储的键名
  storage :storageSession, // 持久化存储引擎
  // 可选的配置项，如白名单、黑名单等 选其一就好了
  blacklist:['contText'], // 只有 test 不会被缓存
  // whitelist: ["test"], // 只有 test 会被缓存
}
 
const persistedReducer = persistReducer(persistConfig, reducer)
 
export const storeData = configureStore({
  reducer: persistedReducer, // 注册子模块
  middleware: (getDefaultMiddleware) =>
    getDefaultMiddleware({
      serializableCheck: false // 关闭默认的序列化检查//关闭严格模式
    })
})
 

//获取store中的所有reducer的数据类型并导出
export type RootState = ReturnType<typeof storeData.getState>
//提取dispatch数据类型
export type AppDispatch = typeof storeData.dispatch;

export const persistor = persistStore(storeData)